package com.lhq.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhq.business.domain.Book;
import com.lhq.business.mapper.BookMapper;
import com.lhq.business.service.IBookService;
import com.lhq.business.service.IChapterService;
import com.lhq.common.core.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 图书Service业务层处理
 *
 * @author lhq
 * @date 2024-05-11
 */
@Service
public class BookServiceImpl  extends BaseServiceImpl<BookMapper,Book>  implements IBookService
{
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private IChapterService chapterService;

    /**
     * 查询图书
     *
     * @param id 图书主键
     * @return 图书
     */
    @Override
    public Book selectById(String id)
    {
        Book book = bookMapper.selectById(id);
        if(book != null){
            book.setChapterCnt(chapterService.selectCountByBookId(book.getId()));
        }
        return book;
    }

    /**
     * 查询图书分页列表
     * @return 图书
     */
    @Override
    public IPage<Book> selectList(Page<Book> page,Book book)
    {
        QueryWrapper<Book> queryWrapper =new QueryWrapper<>();
        return bookMapper.selectPage(page,queryWrapper);
    }

    /**
     * 新增图书
     *
     * @param book 图书
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBook(Book book)
    {
        return bookMapper.insert(book);
    }

    /**
     * 修改图书
     *
     * @param book 图书
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBook(Book book)
    {
        return bookMapper.updateById(book);
    }

    /**
     * 删除图书信息
     *
     * @param id 图书主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(String id)
    {
        Book  book =new Book();
        book.setId(id);
        return bookMapper.update(book,Wrappers
                .lambdaUpdate(Book.class)
                .eq(Book::getId, id)
                .set(Book::getDelFlag, "1"));
    }
}
